package com.foreveross.util;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * Spring辅助类
 */
public class SpringHelper {

    public static ApplicationContext applicationContext = null;

    public static void setApplicationContext(ApplicationContext ctx) {
        applicationContext = ctx;
    }

    public static void setClassPathXmlApplicationContext(String contextPath) {
        if (applicationContext == null) {
            applicationContext = new ClassPathXmlApplicationContext(contextPath);
        }
    }

    public static <T> T getBean(Class<T> clazz) {
        return applicationContext.getBean(clazz);
    }

    public static <T> T getBean(Class<T> clazz, String name) {
        return applicationContext.getBean(name, clazz);
    }

    public static Object getBean(String name) {
        return applicationContext.getBean(name);
    }
}
